//
//  JKPopoverTool.swift
//  afanti
//
//  Created by albert on 16/4/16.
//  Copyright © 2016年 albert. All rights reserved.
//

import UIKit

private var _popView: JKPopoverView?
private var _tool: JKPopoverTool?
private var _titles: [String]?
private var _imageNames: [String]?
private var _highImageNames: [String]?
private var _rowHeight: CGFloat?

class JKPopoverTool: NSObject {
    
    override init() {
        super.init()
        setDataSourceAndDelegate()
    }
    
    /**
     *  设置要显示的popoverView
     *
     *  @param titles         标题数组
     *  @param imageNames     普通图片名称数组
     *  @param highImageNames 高亮图片名称数组
     *  @param popFrame       popover的frame
     *  @param rowHeight      每行的高度
     */
    class func show(titles: [String], imageNames: [String], highImageNames: [String], popFrame: CGRect, rowHeight: CGFloat){
        
        if (_popView == nil) {
            _popView = JKPopoverView()
            _popView!.frame = UIScreen.mainScreen().bounds;
            
            _tool = JKPopoverTool.init()
        }
        
        _titles = titles
        _imageNames = imageNames
        _popView!.popFrame = popFrame
        _rowHeight = rowHeight
        
        _tool!.setDataSourceAndDelegate()
        
        UIApplication.sharedApplication().keyWindow!.addSubview(_popView!)
    }
    
    //设置代理和数据源
    func setDataSourceAndDelegate(){
        _popView!.dataSource = self
        _popView!.delegate = self
    }
}

extension JKPopoverTool: JKPopoverViewDataSource, JKPopoverViewDelegate {
    
    /** 返回多少行 */
    func numberOfRowsInMainTableViewpopoverView(popoverView: JKPopoverView) -> Int{
        return _titles!.count
    }
    
    /** 设置每行的标题 */
    func popoverView(popoverView: JKPopoverView, titleInrow: NSInteger) -> String{
        return _titles![titleInrow]
    }
    
    /** 设置每行的普通图片名称 */
    func popoverView(popoverView: JKPopoverView, imageNameInrow: NSInteger) -> String?{
        
        if _imageNames != nil{
            return _imageNames![imageNameInrow]
        }else{
            return nil
        }
    }
    
    /** 设置每行的高亮图片名称 */
    func popoverView(popoverView: JKPopoverView, hightImageNameInrow: NSInteger) -> String?{
        if _highImageNames != nil{
            return _highImageNames![hightImageNameInrow]
        }else{
            return nil
        }
    }
    
    /** 选中了某一行 */
    func popoverView(popoverView: JKPopoverView, didSelectIndex: NSInteger){
        
        print("选中了第 \(didSelectIndex) 行")
        
        //发通知
        NSNotificationCenter.defaultCenter().postNotificationName(JKPopoverdidSelectNotification, object: nil, userInfo: [JKPopDidSelectIndexKey : didSelectIndex])
        
        _popView = nil
        popoverView.removeFromSuperview()
    }
    
    /** 设置每行的高度 */
    func popoverView(popoverView: JKPopoverView, heightForRowAtIndexPath: NSIndexPath) -> CGFloat{
        if _rowHeight != nil{
            return _rowHeight!
        }else{
            return 44
        }
    }
}